import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class FileTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		//test();
		
		//testFile1();
		
//		copyFile(
//				"S:\\V3WebImage_Storage\\vitech\\dev\\image1.tif", 
//				"S:\\V3WebImage_Storage\\vitech\\\\dev\\image1.tif"
//				);
		
//		test2();
		
		String[] fileNames = getFiles("c:/images", IMAGE_EXTENSION);
		System.out.println("fileNames length: " + fileNames.length);
	}
	
	static List IMAGE_EXTENSION = new ArrayList<String>();
	static {
		// all lower case.
//		IMAGE_EXTENSION.add("tif");
//		IMAGE_EXTENSION.add("jpg");
//		IMAGE_EXTENSION.add("gif");
		IMAGE_EXTENSION.add("ant");
	}

	private static class ImageFileFilter implements FilenameFilter {
		List extList;

		ImageFileFilter(List fileExtension) {
			extList = fileExtension;
		}

		public boolean accept(File dir, String name) {
			if (extList.size() == 0)
				return true; // all files are OK
			String ext = "";
			int index = name.lastIndexOf(".");
			if (index > 0) {
				ext = name.substring(index + 1);
				return extList.contains(ext.toLowerCase());
			}
			return false;
		}
	}

	private static String[] getFiles(String folderName, List fileExtension) {
		if (fileExtension == null)
			fileExtension = IMAGE_EXTENSION;
		File folder = new File(folderName);
		if (folder.exists() && folder.isDirectory()) { // is a folder
			return folder.list(new ImageFileFilter(fileExtension));
		}
		return null;
	}
	
	private static void test2() throws Exception {
		
		String filePath = "/mnt/v3_image_storage/IPERS/batch_folders/systest/export";
		//String filePath = "/temp/my_new_file.txt";
		
		File f = new File(filePath);
		System.out.println(f.getAbsolutePath() + " exists? " + f.exists());
		System.out.println(f.getAbsolutePath() + " is dir? " + f.isDirectory());
		System.out.println(f.getAbsolutePath() + " is file? " + f.isFile());
	}
	
	private static void testFile1() throws Exception {
		File origFile = new File("C:/temp/original.txt");
		origFile.createNewFile();
		
		System.out.println("file " + origFile.getPath() + " exists? " + origFile.exists());

		File destFile = new File("C:/temp/dest.txt");
		
		System.out.println("before renaming - original file path: " + origFile.getAbsolutePath() + 
				" exists? " + origFile.exists());
		
		origFile.renameTo(destFile);
		
		System.out.println("after  renaming - original file path: " + origFile.getAbsolutePath() +
				" exists? " + origFile.exists());
		
		boolean deleted = origFile.delete();
		System.out.println("deleted original file? " + deleted);
		
		
	}

	private static void test() throws IOException, FileNotFoundException {
		File file = new File("c:/no_file.txt");
		System.out.println("file: " + file + (file.exists()? " exists" : " does not exist"));
		
		if (file.createNewFile())
			System.out.println(file + " created.");
		else 
			System.out.println(file + " already exists.");
		
		File file2 = new File("C:\\_TestSpace\\DHTML\\test.html");
		System.out.println("file2.getName(): " + file2.getName());
		
		File file3 = new File("C:\\_TestSpace\\DHTML\\");
		System.out.println("file3.getName(): " + file3.getName());
		
		File newFile = new File("C:\\_TestSpace\\new_file.txt");
		newFile.createNewFile();
		System.out.println("Created " + newFile.getAbsolutePath());

		File file4 = new File("C:\\_TestSpace\\WeeklyDisability.txt");
		BufferedReader br = new BufferedReader(new FileReader(file4));
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
		br.close();
		
		
		File scanDir = new File("\\\\Vitech-projdata\\Common\\Member\\M_Weekly_Dis");
		String[] filenames = scanDir.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.equalsIgnoreCase("test3.TXT");
			}
		});
		System.out.println("filenames = " + Arrays.asList(filenames).toString());
		
		File testFile = new File("\\\\Vitech-projdata\\Common\\Member\\M_Weekly_Dis\\test3.TXT");
		System.out.println(testFile.getName() + " exist? " + testFile.exists());
		testFile.renameTo(new File("\\\\Vitech-projdata\\Common\\Member\\M_Weekly_Dis\\test3.txt"));
		
		
		String unprocessedInputFileName = "C:\\_TestSpace\\dir2\\unprocessed_WeeklyDisability.txt";
		String inputFileName = "C:\\_TestSpace\\dir1\\WeeklyDisability.txt";
		File unprocessedInputFile = new File(unprocessedInputFileName);
		File inputFile = new File(inputFileName);		
		
		//copyFile(unprocessedInputFileName, inputFileName);
		
		if (inputFile.delete()) {
			System.out.println("Deleted input file");
			if (unprocessedInputFile.renameTo(inputFile)) {
				System.out.println("Renamed unprocess input file to input file");
			}
		}
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_hh.mm.ss");
		String timeStamp = dateFormat.format(new Date());
		String sourceFileName = "InputFile.txt";
		String backupFileName = sourceFileName.substring(0, sourceFileName.lastIndexOf(".")) + 
			"_" + timeStamp + sourceFileName.substring(sourceFileName.lastIndexOf("."));
		System.out.println("source file: " + sourceFileName + ", backup file: " + backupFileName);
	}
	
	private static boolean copyFile(String orgFileName, String newFileName) throws IOException {
		
		if (new File(orgFileName).getAbsolutePath().equalsIgnoreCase(new File(newFileName).getAbsolutePath())) {
			System.out.println("source file and destination file are the same");
		}
		
		if (orgFileName.equalsIgnoreCase(newFileName))
			return false; // same place, why bother. if you copy, may scroll up
		FileOutputStream fo = new FileOutputStream(newFileName);
		FileInputStream fi = new FileInputStream(orgFileName);
		byte val[] = new byte[1024];
		while (fi.read(val) != -1) {
			fo.write(val);
		}
		fo.close();
		fi.close();
		return true;
	}
	


}
